package com.courseselector.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.courseselector.entity.course.Course;
import com.courseselector.mapper.CourseMapper;
import com.courseselector.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程服务实现类
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Override
    public Page<Course> getCourseList(Page<Course> page) {
        return this.page(page);
    }

    @Override
    public Course getCourseById(Integer id) {
        return this.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCourse(Course course) {
        // 默认已选人数为0
        course.setEnrolled(0);
        // 默认状态为开放
        course.setStatus(1);
        return this.save(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCourse(Course course) {
        // 获取原课程信息
        Course existCourse = this.getById(course.getId());
        if (existCourse == null) {
            return false;
        }
        // 保持已选人数不变
        course.setEnrolled(existCourse.getEnrolled());
        return this.updateById(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCourse(Integer id) {
        return this.removeById(id);
    }
} 